//
//  WHY UserAcount.swift
//  WHYWeibo-1.0
//
//  Created by 王洪运 on 15/11/24.
//  Copyright © 2015年 王洪运. All rights reserved.
//

import UIKit

class WHYUserAccount: NSObject, NSCoding {
    
 /// 令牌
    var access_token: String?
 /// 令牌的声明周期
    var expires_in: NSTimeInterval = 0 {
        didSet {
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
 /// 用户的UID
    var uid: String?
/// 过期日期
    var expiresDate: NSDate?
/// 用户昵称
    var screen_name: String?
/// 用户头像地址
    var avatar_large: String?
    
    
    /// 字典转模型
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
/// 保存用户到沙盒
//    func saveUserAccount() {
//        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
//        QL2(path)
//        
//        NSKeyedArchiver.archiveRootObject(self, toFile: path)
//    }
    
// MARK: - 归档和解档
    /// 归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    /// 解档
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        
    }
    
    override var description: String {
        let key = ["access_token", "expires_in", "uid", "expiresDate", "screen_name", "avatar_large"];
        
        return dictionaryWithValuesForKeys(key).description
    }

}


